/* smog_pollution_analysis_per_sqmile: This script repeats table 2 from the paper, but using
reinspections per urban square mile, rather than the level of re-inspections*/

capture log close

clear all

set more off
set matsize 2000

cd ${path}appendix

log using smog_pollution_analysis.log, replace


use ../analysis/pollution_star_merged

xtset county date

merge m:1 county using urban_by_county, keep(master matched) nogen

/*First, divide the counts of inspections by 1000, to make the coefficients easier to read*/

gen lag = 0
foreach var of varlist /*star* re_pass* directed* asm**/ *pass* firstgen* secondgen* thirdgen* othergen* initial_inspection* N{
	qui replace `var' = (`var')/area
}

gen fail_count_90 = initial_inspection_90 - pass_90
/* Generate day-of-week and calendar week*/

gen dow = dow(date)
gen week = wofd(date)

gen newcars = _n>20 in 1/40
gen bin = _n-1 -20*(_n>20) in 1/40

/*Declare the "if" conditions in a local, as well as the full set of controls*/

local conditions  if year>=1998 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003)) 
local controls tMin tMax prec ib1.county /*ib1.county#(c.date##c.date##c.date)*/ ib1.county#i.year i.week 




/*Create some index variables for storing the "bin" coefficients"*/




capture program drop estadd_oldnewtest
program estadd_oldnewtest, eclass
	test firstgen_90 = othergen_90
	ereturn scalar oldnew_F = r(F)
	ereturn scalar oldnew_p = r(p)
end


/*Now run regressions for each pollutant*/

foreach pollutant in nox co {

	/*For table captions, the uppercase abbreviation of the pollutant name*/

	local upper = cond("`pollutant'"=="ozone",proper("`pollutant'"),upper(substr("`pollutant'",1,2))+substr("`pollutant'",3,.))

	/*Multiply the pollutant variables by 1000 to change to ppb (again, to make coefficients readable*/

	replace `pollutant' = `pollutant'*1000
	
	/* Ozone should control for contemporaneous NOx*/
	


	
	/*First set of regressions: Using all re-inspections, build up controls */
	
	local counter = 1
	
	eststo `pollutant'_buildup`counter++': regress `pollutant' firstgen_90 othergen_90 `conditions', vce(cluster county)
	eststo `pollutant'_buildup`counter++': regress `pollutant' firstgen_90 othergen_90 ib1.county i.week `conditions', vce(cluster county)
	//eststo `pollutant'_buildup`counter++': regress `pollutant' firstgen_90 othergen_90 ib1.county i.week ib1.county#(c.date) `conditions', vce(cluster county)
	//eststo `pollutant'_buildup`counter++': regress `pollutant' firstgen_90 othergen_90 ib1.county i.week ib1.county#(c.date##c.date) `conditions', vce(cluster county)
	//eststo `pollutant'_buildup`counter++': regress `pollutant' firstgen_90 othergen_90  ib1.county i.week ib1.county#c.date ib1.county#c.date#c.date ib1.county#c.date#c.date#c.date `conditions', vce(cluster county) 
	
	eststo `pollutant'_buildup`counter++': regress `pollutant' firstgen_90 othergen_90 `controls'  `conditions', vce(cluster county)
	eststo `pollutant'_base
	
	/* Store tests of equality between old and new cars*/
	
	estadd oldnewtest: `pollutant'_buildup* `pollutant'_base
	


}




//replace bin = bin*.25

/* Table 2 : Build-up, with CO in panel 1 and NOx in panel 2*/
#delimit ;

esttab co_buildup* using buildup_sqmile.tex, replace se booktabs label nomtitles star( * .1 ** .05 *** .01)
indicate("County FE = *.county"  "Calendar Week FE = *.week" 
/*"County Linear Trends = *y#c.date" "County Quadratic Trends = *y#c.date#c.date" "County Cubic Trends = *y#c.date#c.date#c.date"*/ "County-Year FE = *.year" "Weather Controls = tMin tMax prec"  ) 
title(Re-Inspections and County-Level Daily Air Quality\label{tab:buildup_sqmile})
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")
varlabels(firstgen_90 "Re-Inspections/Urban mi\$^2\$\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles" fail_count_90 "000s of failures last 90 days")
postfoot( ) nocons  mgroup("A: Outcome is Carbon Monoxide (PPB)", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}))
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
;

esttab nox_buildup* using buildup_sqmile, append se booktabs label nomtitles  star( * .1 ** .05 *** .01)
indicate("County FE = *.county"  "Calendar Week FE = *.week" 
/*"County Linear Trends = *y#c.date" "County Quadratic Trends = *y#c.date#c.date" "County Cubic Trends = *y#c.date#c.date#c.date"*/ "County-Year FE = *.year" "Weather Controls = tMin tMax prec"  ) 
mgroup("B: Outcome is \nox\ (PPB)", pattern(1 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}))
scalars("oldnew_F F-test of Equality" "oldnew_p \$ P > F\$")
varlabels(firstgen_90 "Re-Inspections/Urban mi\$^2\$\\ \quad\quad 1975-1985 Vehicles"  othergen_90 "\quad\quad 1985+ Vehicles" fail_count_90 "000s of failures last 90 days")
prehead(\midrule )  nocons postfoot(\bottomrule @starlegend \\ \multicolumn{@span}{l}{\footnotesize @note} \end{tabular}\end{table})
note(Note: Observations are county-days.  Standard errors clustered by county reported in parentheses.  F-tests of equality report the F-statistics from testing the null hypothesis that the coefficient on 1975--1985 model year vehicles equals the coefficient on 1985+ model year vehicles. )
substitute( "\begin{tabular}" "\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}"
            "\end{tabular}" "\end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents"
            "{l}{\footnotesize" "{p{\linewidth}}{\footnotesize")
;


#delimit cr



